//
//  YYETextView.swift
//  weibo14
//
//  Created by mac on 16/3/11.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

@IBDesignable
class YYETextView: UITextView {

    
//    override var text: String? {
//        didSet{
//    
//        }
//    }
    @IBInspectable var placeholder:String? {
    
        didSet{
        
            placeHolderLabel.text = placeholder
        }
    }
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
       
        super.init(coder: aDecoder)
        setupUI()
    }
    
    private func  setupUI() {
    
       self.alwaysBounceVertical = true
       self.alwaysBounceHorizontal = false
        
        addSubview(placeHolderLabel)
        
        placeHolderLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(self).offset(5)
            //  make.trailing.equalTo(self).offset(-5)
            make.width.equalTo(SCREENW - 10)
            make.top.equalTo(10)
        }
        
        // 去监听他的值改变
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textDidChanged", name: UITextViewTextDidChangeNotification, object: nil)
        
    }

    @objc func textDidChanged() {
    
        placeHolderLabel.hidden = self.hasText()
    }
    
    

    
    // MARK: - 懒加载控件
    private lazy var placeHolderLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.lightGrayColor()
        // 设置默认的字体大小
        label.font = UIFont.systemFontOfSize(12)
        // 多行
        label.numberOfLines = 0
        return label
    }()
    
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

}






